#!/usr/bin/env python
#coding: utf8 

##TODO: Mess with speech threshold, tweak face recognition
###################

import random
import subprocess
from humia_facerec import *
import humia_speech
import humia_facerec
from humia_process import *
from GPIO import *
import termios
import sys
import tty

EXIT,EXIT2,CMD,FFLAG = 0,0,0,0
CMD_TEXT = ''
def ping_main():
    Ping = GPIO(57)
    Ping.mode('in')
    ChngTime = datetime.now()
    Last_State = 0
    TCount = 0
    FCount = 0
    while(True):
	delta = datetime.now() - ChngTime
	if ((int)(Ping.read())) == 1:
	    TCount += 1
	    if Last_State == 0 and (float)(delta.total_seconds()) > 1.5 and TCount >15:
		    humia_speech.PING = 1
		    humia_facerec.PING = 1
		    Last_State = 1
		    ChngTime = datetime.now()
		    TCount, FCount = 0,0
	elif ((int)(Ping.read())) == 0:
	    FCount += 1
	    if Last_State == 1 and (float)(delta.total_seconds()) > 1.5 and FCount >15:
		    humia_speech.PING = 0
		    humia_facerec.PING = 0
		    Last_State = 0
		    ChngTime = datetime.now()
		    TCount, FCount = 0,0
	time.sleep(0.1)
    return


def speak(text):
	subprocess.call('echo '+str(text)+' | festival --tts', shell=True)
	return

def init_main():
    #init_serial()
    thread.start_new_thread(ping_main,())
    thread.start_new_thread(humia_speech.speech_rec_main,())
    thread.start_new_thread(facemain,())
 
    return

if __name__ == '__main__':
    init_main()
    time.sleep(3)
    t_ans,m_ans,qst=  load_data()
 
    while(True):
	if(humia_speech.FLAG == 1 and humia_facerec.FLAG == 1):
	    x = random.random()
            if(x <0.5):
	        # Speech
	        humia_speech.SPEAK = True
	        humia_facerec.SPEAK = True
	        i = find_ans(str(humia_speech.TEXT),qst)
	        subprocess.call('flite -o out.wav -voice rms -t "'+str(t_ans[i])+'"', shell=True)
	        thread.start_new_thread(play_ans, (m_ans[i],))
		
	        subprocess.call('mplayer -af scaletempo -speed 0.6 out.wav', shell=True)
	        time.sleep(2);
	        humia_facerec.SPEAK = False
	        humia_speech.SPEAK = False
	        humia_speech.FLAG = 0
	    else:
	        humia_speech.SPEAK = True
	        humia_facerec.SPEAK = True
	        i = find_ans(str(humia_facerec.NAME),qst)
	        subprocess.call('flite -o out.wav -voice rms -t "'+str(t_ans[i])+'"', shell=True)
	        thread.start_new_thread(play_ans, (m_ans[i],))
		
	        subprocess.call('mplayer -af scaletempo -speed 0.6 out.wav', shell=True)

	        humia_facerec.SPEAK = False
	        humia_speech.SPEAK = False
	        humia_facerec.FLAG = 0

	elif(humia_speech.FLAG == 1):
	    # Speech
	    humia_speech.SPEAK = True
	    humia_facerec.SPEAK = True
	    i = find_ans(str(humia_speech.TEXT),qst)
	    subprocess.call('flite -o out.wav -voice rms -t "'+str(t_ans[i])+'"', shell=True)
	    thread.start_new_thread(play_ans, (m_ans[i],))
		
	    subprocess.call('mplayer -af scaletempo -speed 0.6 out.wav', shell=True)
	    time.sleep(2);
	    humia_facerec.SPEAK = False
	    humia_speech.SPEAK = False
	    humia_speech.FLAG = 0

	elif(humia_facerec.FLAG == 1):
	    humia_speech.SPEAK = True
	    humia_facerec.SPEAK = True
	    i = find_ans(str(humia_facerec.NAME),qst)
	    subprocess.call('flite -o out.wav -voice rms -t "'+str(t_ans[i])+'"', shell=True)
	    #thread.start_new_thread(play_ans, (m_ans[i],))
		
	    subprocess.call('mplayer -af scaletempo -speed 0.6 out.wav', shell=True)

	    humia_facerec.SPEAK = False
	    humia_speech.SPEAK = False
	    humia_facerec.FLAG = 0

	    time.sleep(2);
	time.sleep(0.5)

